/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class YQuery {
    private String select=null;
    private String[] fetchProperties=null;
    private String defaultWhere=null;
    private Object[] defaultArgs=null;
    private List<YCriteria> criterias=new ArrayList(3);

    public String getSelect() {
        return select;
    }

    public void setSelect(String select) {
        this.select = select;
    }

    public String[] getFetchProperties() {
        return fetchProperties;
    }

    public void setFetchProperties(String[] fetchProperties) {
        this.fetchProperties = fetchProperties;
    }

    public String getDefaultWhere() {
        return defaultWhere;
    }

    public void setDefaultWhere(String defaultWhere) {
        this.defaultWhere = defaultWhere;
    }

    public List<YCriteria> getCriterias() {
        return criterias;
    }

    public void setCriterias(List<YCriteria> criterias) {
        this.criterias = criterias;
    }

    public boolean addCriteria(YCriteria yCriteria) {
        return criterias.add(yCriteria);
    }
    public boolean addCriteria(String  yCriteriaString,Object... args) {
        return criterias.add(new YCriteria(yCriteriaString, args));
    }

    public Object[] getDefaultArgs() {
        return defaultArgs;
    }

    public void setDefaultArgs(Object[] defaultArgs) {
        this.defaultArgs = defaultArgs;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof YQuery)) return false;

        YQuery yQuery = (YQuery) o;

        if (criterias != null ? !criterias.equals(yQuery.criterias) : yQuery.criterias != null) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(defaultArgs, yQuery.defaultArgs)) return false;
        if (defaultWhere != null ? !defaultWhere.equals(yQuery.defaultWhere) : yQuery.defaultWhere != null)
            return false;
        if (!Arrays.equals(fetchProperties, yQuery.fetchProperties)) return false;
        if (select != null ? !select.equals(yQuery.select) : yQuery.select != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = select != null ? select.hashCode() : 0;
        result = 31 * result + (fetchProperties != null ? Arrays.hashCode(fetchProperties) : 0);
        result = 31 * result + (defaultWhere != null ? defaultWhere.hashCode() : 0);
        result = 31 * result + (defaultArgs != null ? Arrays.hashCode(defaultArgs) : 0);
        result = 31 * result + (criterias != null ? criterias.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("YQuery");
        sb.append("{select='").append(select).append('\'');
        sb.append(", fetchProperties=").append(fetchProperties == null ? "null" : Arrays.asList(fetchProperties).toString());
        sb.append(", defaultWhere='").append(defaultWhere).append('\'');
        sb.append(", defaultArgs=").append(defaultArgs == null ? "null" : Arrays.asList(defaultArgs).toString());
        sb.append(", criterias=").append(criterias);
        sb.append('}');
        return sb.toString();
    }
}


